import {
    Inject, Controller, Post, Get,
    Body, Query
} from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { PermissionService } from '../service/permission.service';
import { Permission } from '../entity/permission.entity';

@Controller('/permission')
export class PermissionController {
    @Inject()
    ctx: Context;

    @Inject()
    permissionService: PermissionService;

    @Post('/create')
    async create(@Body() permission: Partial<Permission>) {
        try {
            const result = await this.permissionService.create(permission);
            return {
                code: 200,
                message: '创建成功',
                data: result
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '创建失败'
            };
        }
    }

    @Post('/update')
    async update(@Body() permission: Partial<Permission>) {
        try {
            const success = await this.permissionService.update(permission.id, permission);
            return {
                code: 200,
                message: success ? '更新成功' : '更新失败'
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '更新失败'
            };
        }
    }

    @Get('/del')
    async delete(@Query('id') id: number,) {
        try {
            const success = await this.permissionService.delete(id);
            return {
                code: 200,
                message: success ? '删除成功' : '删除失败'
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '删除失败'
            };
        }
    }

    @Get('/detail')
    async getInfo(@Query('id') id: number) {
        try {
            const permission = await this.permissionService.getInfo(id);
            return {
                code: 200,
                message: '获取成功',
                data: permission
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '获取失败'
            };
        }
    }

    @Get('/list')
    async getList(
        @Query('page') page: number,
        @Query('pageSize') pageSize: number,
        @Query('permName') permName?: string,
        @Query('permCode') permCode?: string,
        @Query('status') status?: number,
        @Query('parentId') parentId?: number
    ) {
        try {
            const [list, total] = await this.permissionService.getList({
                page,
                pageSize,
                permName,
                permCode,
                status,
                parentId
            });

            return {
                code: 200,
                message: '获取成功',
                data: list,
                total
                // data: {
                //     list,
                //     pagination: {
                //         current: page,
                //         pageSize,
                //         total
                //     }
                // }
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '获取失败'
            };
        }
    }

    @Get('/tree')
    async getPermissionTree() {
        try {
            const tree = await this.permissionService.getPermissionTree();
            return {
                code: 200,
                message: '获取成功',
                data: tree
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '获取失败'
            };
        }
    }

    @Get('/children')
    async getChildren(@Query('parentId') parentId: number) {
        try {
            const children = await this.permissionService.getChildrenByParentId(parentId);
            return {
                code: 200,
                message: '获取成功',
                data: children
            };
        } catch (error) {
            return {
                code: 500,
                message: error.message || '获取失败'
            };
        }
    }
}